<%@ page language="java" contentType="text/javascript; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>

<%
	response.setHeader("Access-Control-Allow-Origin", "*");
	response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String name = request.getParameter("name");
	String postnum = request.getParameter("num");
	String text =  request.getParameter("text");
	//String text =  new String(request.getParameter("text").getBytes("8859_1"), "utf-8");
	String date = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
	String time = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date());
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	JSONObject jo = new JSONObject();
	
	try{
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection(
			"jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
		
		String insert = "insert into PostComment" +
						"(PostNumber, WriterID, Writer, Text, Date, Time)" +
						"values (?, ?, ?, ?, ?, ?);";
		pstmt = conn.prepareStatement(insert);
		pstmt.setString(1, postnum);
		pstmt.setString(2, id);
		pstmt.setString(3, name);
		pstmt.setString(4, text);
		pstmt.setString(5, date);
		pstmt.setString(6, time);
		pstmt.executeUpdate();
		
		
		jo.put("success", "true" );
		out.print(jo);
	} catch (Exception e) {
		jo.put("success", "true" );
		jo.put("error", e );
		out.print(jo);
	}
	
	finally{
		try{
			pstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			conn.close();
		}
		catch (Exception ignored){
		}
	}
%>